package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.mapper.CategoryMapper;
import com.neuedu.pojo.Category;
import com.neuedu.service.CateParamsService;
import com.neuedu.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CateParamsService cateParamsService;
    /**
     * 递归获取当前对象的所有结点
     * @param category
     * @return
     */
    @Override
    public void getAllChildren(Category category){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, category.getId())
                .orderByDesc(Category::getSort);
        List<Category> categories = super.list(queryWrapper);
        if(categories != null && categories.size() > 0){
            category.setChildren(categories);
            categories.forEach(this::getAllChildren);
        }
    }

    @Override
    public void deleteWithChildren(Category category) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, category.getId());
        List<Category> categories = super.list(queryWrapper);
        super.removeById(category.getId());
        if(categories != null && categories.size() > 0){
            categories.forEach(this::deleteWithChildren);
        }

    }


}
